package view;

import domain.Employee;
import domain.Programmer;
import service.NameListService;
import service.TeamException;
import service.TeamService;

public class TeamView {
	private NameListService listSvc = new NameListService();
	private TeamService teamSvc = new TeamService();

	public void enterMainMenu() {
		boolean isFlag = true;
		char menu = 0;
		while (isFlag) {
			if (menu != '1') {
				listAllEmployee();
			}
			System.out.print("1-?????? 2-???????? 3-???????? 4-??? ?????(1-4)??");
			menu = TSUtility.readMenuSelection();
			switch (menu) {
			case '1':
				getTeam();
				break;
			case '2':
				addMember();
				break;
			case '3':
				deleteMember();
				break;
			case '4':
				System.out.print("?????????(Y/N)??");
				char isExit = TSUtility.readConfirmSelection();
				if (isExit == 'Y') {
					isFlag = false;
				}
				break;
			}
		}
	}

	private void listAllEmployee() {
		System.out.println("------------------------------?????????????------------------------------\n");
		Employee[] employees = listSvc.getAllEmployees();

		if (employees == null || employees.length == 0) {
			System.out.println("???????????????????????");
		} else {
			System.out.println("ID\t????\t????\t????\t??\t??\t????\t???\t??????");
			for (int i = 0; i < employees.length; i++) {
				System.out.println(employees[i]);
			}
		}
		System.out.println("-----------------------------------------------------------------------\n");
	}

	// ??????????
	private void getTeam() {
		System.out.println("--------------------????????--------------------\n");
		Programmer[] team = teamSvc.getTeam();
		if (team.length == 0 || team == null) {
			System.out.println("?????????????????????");
		} else {
			System.out.println("TID/ID\t????\t????\t????\t??\t????\t???\n");
			for (int i = 0; i < team.length; i++) {
				System.out.println(team[i].getDetailsForTeam());
			}
		}
		System.out.println("------------------------------------------------\n");
	}

	// ?????
	private void addMember() {
		System.out.println("--------------------?????--------------------");
		System.out.print("????????????ID??");
		int id = TSUtility.readInt();
		try {
			Employee emp = listSvc.getEmployee(id);
			teamSvc.addMember(emp);
			System.out.println("???????????");
		} catch (TeamException e) {
			System.out.println("??????????" + e.getMessage());
		}
		// ???????????
		TSUtility.readReturn();
	}

	private void deleteMember() {
		System.out.println("--------------------??????--------------------");
		System.out.print("???????????????TID??");
		int memberId = TSUtility.readInt();
		System.out.print("?????????(Y/N)??");

		char isDelete = TSUtility.readConfirmSelection();
		if (isDelete == 'N') {
			return;
		}
		try {
			teamSvc.removeMember(memberId);
			System.out.println("????????????");
		} catch (TeamException e) {
			System.out.println("??????????" + e.getMessage());
		}
		// ???????????
		TSUtility.readReturn();
	}

	public static void main(String[] args) {
		TeamView view = new TeamView();
		view.enterMainMenu();
	}
}
